-- develop a system for representing geometric regions,
-- where regions were collections of points

-- geometric region
function disk (cx, cy, r)
	return function (x, y)
		return (x - cx)^2 + (y - cy)^2 <= r^2
	end
end

function rect (left, right, bottom, up)
	return function (x, y)
		return left <= x and x <= right and 
					 bottom <= y and y <= up
	end
end

-- set operation
function complement (region)
	return function (x, y)
		return not region(x, y)
	end
end

function union (region1, region2)
	return function (x, y)
		return region1(x, y) or region2(x, y)
	end
end

function intersection (region1, region2)
	return function (x, y)
		return region1(x, y) and region2(x, y)
	end
end

function difference (region1, region2)
	return function (x, y)
		return region1(x, y) and not region2(x, y)
	end
end

function translate (region, dx, dy)
	return function (x, y)
		return region(x - dx, y - dy)
	end
end

function plot (region, M, N)
	io.write("P1\n", M, " ", N, "\n")
	for i = 1, N do
		local y = (N - i * 2) / N
		for j = 1, M do
			local x = (j * 2 - M) / M
			io.write(region(x, y) and "1" or "0")
		end
		io.write("\n")
	end
end

c1 = disk(0, 0, 1)
plot(difference(c1, translate(c1, 0.3, 0)), 100, 50)
